<template>
	<view class="container">
		<view class="head">
			<view class="text">购物车({{useful.length}})</view>
			<view class="text" style="color:#ff3b3b ;" @click="manageClick" v-if="manage == false">管理</view>
			<view class="text" style="color:#ff3b3b ;" @click="manageClick" v-else>完成</view>
		</view>
		<view class="content">
			<view v-for="(item,index) in useful" :key="index">
				<view class="list_box">
					<view class="content_con">
						<view class="content_boxs">
							<view class="select" style="margin-top: 46rpx;" @click="danSelect(index)">
								<u-icon name="checkmark-circle-fill" v-if="item.select" size="44" color="#ff3b3b">
								</u-icon>
								<view class="noselect" v-else></view>
							</view>
							<view style="flex: 1;width: 580rpx;">
								<view style="flex: 1;width: 580rpx; margin-bottom: 20rpx;">
									<view>
										<view class="content_box">
											<view class="img_box">
												<image :src="item.pics" mode="aspectFill"></image>
											</view>
											<view class="big_infos">
												<view class="name">
													<view>{{item.item_name}}</view>
												</view>
												<view class="sku">
													<view>{{item.item_spec_desc}}</view>
												</view>
												<view class="num">
													<view class="price">￥{{(item.price/100).toFixed(2)}}</view>
													<view class="number">
														<u-number-box v-model="item.num"
															@change="valChange(index,$event)" :min="1">
														</u-number-box>
													</view>
												</view>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="bottom" v-if="manage == false" @click="bayShop">
			<view class="total">共{{number}}件，合计：<text>￥{{(total/100).toFixed(2)}}</text></view>
			<view class="btn" @click="goPay">立即购买</view>
		</view>
		<view class="bottom" v-else>
			<view class="total" style="display: flex;align-items: center;" @click="allselect">
				<view class="select" style="margin-right: 20rpx;" v-if="selsect == true">
					<u-icon name="checkmark-circle-fill" size="44" color="#ff3b3b"></u-icon>
				</view>
				<view class="select" style="margin-right: 20rpx;" v-else>
					<view class="noselect"></view>
				</view>
				全选
			</view>
			<view class="btn" @tap="delectClick">删除</view>
		</view>
		<u-modal v-model="show" :content="content" @confirm="deletCart"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: 1,
				shopArr: [],
				show: false,
				content: '确定删除所选商品吗？',
				selsect: false,
				manage: false,
				useful: [],
				failure: [],
				total: 0,
				number: 0,
			}
		},
		onShow() {
			this.getData()
			this.total = 0
			this.number = 0
		},
		methods: {
			async getData() {
				uni.showLoading({
					title: '加载中'
				})
				await this.$allApi.cartList().then(res => {
					if (res.data.valid_cart.length == 0) {
						this.useful = []
					} else {
						this.useful = res.data.valid_cart[0].list
						for (let item of this.useful) {
							item.select = false
							item.total = item.price * item.num
						}
					}
					uni.hideLoading()
				})
			},
			danSelect(index) { //单买
				this.useful[index].select = !this.useful[index].select
				let yes = this.useful.filter(item => {
					return item.select == true
				})
				if (this.useful[index].select == true) {
					this.number++
					this.total = this.total + this.useful[index].total
					if (yes.length == this.useful.length) {
						this.selsect = true
						this.useful[index].select = true
					}
				} else {
					this.total = this.total - this.useful[index].total
					if (yes.length < this.useful.length) {
						this.selsect = false
						this.useful[index].select = false
						this.number--
					}
				}

				if (this.total < 0) {
					this.total = 0
				}
				this.$forceUpdate()
			},
			valChange(index) { //数量增加
				let money = 0
				this.useful[index].total = this.useful[index].price * this.useful[index].num
				for (let item of this.useful) { //数量增加就循环整个购物车
					if (item.select == true) { //选中物品得价格累加
						money += item.total
					}
				}
				this.total = money
				this.$forceUpdate()
			},
			manageClick() { //点击管理
				this.manage = !this.manage
				let select = 0
				let length = 0
				for (let item of this.useful) {
					if (item.select == true) {
						select++
					}
				}
				if (select == this.useful.length) {
					this.selsect = true
				} else {
					this.selsect = false
				}
			},
			allselect() {
				this.selsect = !this.selsect
				let money = 0
				if (this.selsect == true) {
					for (let item of this.useful) {
						item.select = true
						item.total = item.price * item.num
						money += item.total
					}
					this.total = money
					this.number = this.useful.length
				} else {
					for (let item of this.useful) {
						item.select = false
						item.total = item.price * item.num
						money += item.total
					}
					this.total = this.total - money
					this.number = 0
					if (this.total < 0) {
						this.total = 0
					}
				}
			},
			delectClick() { //点击删除
				this.show = true
			},
			goPay() {
				let orderPayList = []
				for (let item of this.useful) {
					if (item.select == true) {
						orderPayList.push(item)
					}
				}
				if (orderPayList.length == 0) {
					this.$u.toast('请选择您要结算的商品')
					return
				} else {
					let newObj = {
						data: orderPayList,
						total: this.total
					}
					uni.setStorageSync('orderPayData', JSON.stringify(newObj))
					this.$gourl('/pages/pay/orderPay')
				}
				// if()
			},
			deletCart() {
				uni.showLoading({
					title: '删除中'
				})
				let cart = []
				for (let item of this.useful) {
					if (item.select == true) {
						cart.push(item.cart_id)
					}
				}
				this.$allApi.cartDel({
					cart_id: cart.join(',')
				}).then(res => {
					uni.hideLoading()
					this.total = 0
					this.select = false
					this.number = 0
					this.getData()
				})
			},
			bayShop() {
				let shops = []
				let shop_num = 0
				let dan = {
					group_id: '',
					goods_list: []
				}
				let tuan = {}
				for (let item of this.useful) {
					for (let item2 of item.group) {
						for (let item3 of item2.cart) {
							if (item2.is_single == 1) {
								if (item3.select == true) {
									dan.group_id = item2.id
									dan.invitation_id = item2.cart[0].invitation_id
									dan.goods_list.push({
										goods_id: item3.goods_id,
										sku_price_id: item3.sku_price_id,
										goods_num: item3.goods_num,
									})
									shops.push(dan)
									shop_num++
								}
							} else {
								console.log('item2', item2)
								if (item2.select == true) {
									tuan.invitation_id = item2.cart[0].invitation_id
									tuan.group_id = item2.id
									tuan.num = item3.goods_num
									shops.push(tuan)
									shop_num++
								}
							}
						}
					}
				}
				if (shops.length == 0) {
					this.$u.toast('请选择您要结算的商品')
					return
				}
				let aaa = this.quchong(shops)
				let datas = {
					shop: aaa,
					type: 'cart',
					shop_num: shop_num
				}
				this.$u.api.getShopPrice({
					group_list: datas.shop,
					from: this.type,
					address_id: 0,
				}).then(res => {
					getApp().shoppingInfo = JSON.stringify(datas)
					// uni.setStorageSync('shoppingInfo', JSON.stringify(datas))
					this.$gourl('/order/shopping/group')
					uni.hideLoading()
				})

			},
			quchong(arr) {
				let res = {}
				let map = new Map();
				for (let item of arr) {
					if (!map.has(item.group_id)) {
						map.set(item.group_id, item);
					}
				}
				return [...map.values()];
				return Object.values(res)
			}
		}
	}
</script>

<style scoped lang="scss">
	.types {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.noselect {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		border: #ccc 1px solid;
	}


	// #ifdef H5
	.container {
		min-height: calc(100vh - 94px) !important;
	}

	// #endif
	.container {
		min-height: 100vh;
		background: #f6f6f6;
		position: relative;
	}

	.head {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 90rpx;
		background: #fff;
		font-size: 28rpx;
		padding: 0 30rpx;
	}

	.content {
		padding: 30rpx;
		padding-bottom: 120rpx;

		.select {
			width: 70rpx;
		}

		.list_box {
			padding: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
		}

		.list_box {
			margin-bottom: 30rpx;
		}

		.content_con {


			.head {
				padding: 20rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1px solid #eee;

				.left {
					display: flex;
					align-items: center;

					image {
						width: 50rpx;
						height: 50rpx;
						border-radius: 10rpx;
						margin-right: 20rpx;
					}
				}
			}

			.content_boxs {
				margin-top: 20rpx;
				display: flex;
				// align-items: center;
				justify-content: space-between;
			}

			.content_box {
				flex: 1;
				display: flex;
				justify-content: space-between;

				.img_box {
					width: 140rpx;
					height: 140rpx;

					image {

						width: 140rpx;
						height: 140rpx;
						border-radius: 20rpx;
					}
				}

				.big_infos {
					flex: 1;
					padding-left: 20rpx;

					.name {
						font-size: 32rpx;
						display: flex;
						justify-content: space-between;
						align-items: center;
					}

					.sku {
						font-size: 24rpx;
						color: #999;
						display: flex;
						line-height: 2;
						justify-content: space-between;
						align-items: center;
					}

					.num {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.price {
							color: red;
							font-weight: bold;
						}
					}
				}
			}

			.small_info {
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				image {
					width: 95rpx;
					height: 95rpx;
					border-radius: 10rpx;
				}

				.infos {
					flex: 1;
					padding-left: 20rpx;
					width: 596rpx;
					line-height: 1.8;

					.name {
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}

					.miaosu {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
		}
	}

	.bottom {
		height: 100rpx;
		background: #ffff;
		box-shadow: 0px 2px 5px 2px #eee;
		position: absolute;
		bottom: 0;
		left: 0;
		z-index: 99;
		width: 100vw;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.total {
			text {
				color: red;
				font-weight: bold;
			}

			padding-left: 30rpx;
		}

		.btn {
			height: 100%;
			width: 220rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			background: #ff3b3b;
		}
	}
</style>